package com.base.app_core.View;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;

import com.base.app_core.R;

/**
 * Created by quantan.liu on 2017/3/14.
 */

public class LoadingView extends android.support.v7.widget.AppCompatImageView {
    private int mWidth;
    private int mHeight;
    private Bitmap bitmap;
    private int bitmapLeft;
    private int bitmapTop;
    private boolean circle=true;
    public LoadingView(Context context) {
        this(context, null);
    }

    public LoadingView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    public void setCircle(boolean circle) {
        this.circle = circle;
        if(circle){
            startRotate();
        }else{
            stopRotate();
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if(circle)
            startRotate();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        stopRotate();
    }

    /**
     * 开启动画
     */
    public void startRotate(){
        Animation operatingAnim = AnimationUtils.loadAnimation(getContext(), R.anim.loadingroate);
        LinearInterpolator lin = new LinearInterpolator();
        operatingAnim.setInterpolator(lin);
        if(operatingAnim!=null){
            startAnimation(operatingAnim);
        }
    }
    /**
     * 关闭动画
     */
    public void stopRotate(){
        clearAnimation();
    }
}
